﻿using Footy.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Footy.Controllers
{
    public class HomeController : GlobalController
    {
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(User user)
        {
            using(var context = FootyDB.CreateContext())
            {
                var dbUser = context.Users.Where(u => u.Username == user.Username).FirstOrDefault();
                if(dbUser == null)
                {
                    return Json("Tên đăng nhập không tồn tại");
                }
                else if(dbUser.Password != user.Password)
                {
                    return Json("Mật khẩu không đúng");
                }
                else
                {
                    Session[Constants.Session_UserName] = dbUser.Username;
                    Session[Constants.Session_TeamName] = dbUser.TeamName;
                    Session[Constants.Session_UserID] = dbUser.ID;
                    if(user.IsRememberLogin)
                    {
                        //Response.Cookies[Constants.Session_UserID].Secure = true;
                        Response.Cookies[Constants.Session_UserID].Value = dbUser.ID.ToString();
                        Response.Cookies[Constants.Session_UserID].Expires = DateTime.UtcNow.AddDays(30).Date;
                    }
                    return Json(dbUser);
                }
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Logout()
        {
            Session.Remove(Constants.Session_UserID);
            var cookie = Response.Cookies[Constants.Session_UserID];
            if(cookie != null)
            {
                cookie.Expires = DateTime.UtcNow.AddDays(-2);
                cookie.Value = string.Empty;
                Response.Cookies.Add(cookie);
            }
            return Json(true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user">An user want to add with ID=0</param>
        /// <returns>Return an User object if success, otherwise return a string to description error or guide how to continue.</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateUser(User user)
        {
            using (var context = FootyDB.CreateContext())
            {
                //TODO: validate input

                bool isExists = context.Users.Any(u => u.Username == user.Username || u.TeamName == user.TeamName);
                if(isExists)
                {
                    return Json(string.Format("Tên đội bóng hoặc tên đăng nhập đã tồn tại"));
                }

                context.Users.Add(user);
                if (context.SaveChanges() > 0)
                {
                    return Json(user);
                }
                else
                {
                    return Json("Vui lòng thử lại");
                }
            }
        }
    }
}
